﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Shoozla.ViewModel;
using Shoozla.DataModel;
using System.Threading;

namespace Shoozla.View
{
    /// <summary>
    /// Interaction logic for PreviewView.xaml
    /// </summary>
    public partial class PreviewView : UserControl
    {
        private ProgressBarView _progressView;

        public PreviewView()
        {
            InitializeComponent();
            PreviewViewModel previewList = new PreviewViewModel();
            DataContext = previewList;
            previewList.ItemRead += new EventHandler(folderList_FileRead);
            previewList.ItemOperationStarted += new ViewModelBase.ItemOperationStartedHandler(folderList_FileOperationStarted);
            previewList.FileSaved += new ViewModelBase.CoverSavedHandler(previewList_FileSaved);
        }

        void previewList_FileSaved(string filename)
        {
            App.Logger.FilesSaved = (App.Logger.FilesSaved + 1);
        }

        void folderList_FileOperationStarted(int num, string action)
        {

            Thread newWindowThread = new Thread(new ParameterizedThreadStart(StartProgressWindowInternal));
            newWindowThread.SetApartmentState(ApartmentState.STA);
            newWindowThread.IsBackground = true;
            newWindowThread.Start(new object[] { num, action });
        }

        private void StartProgressWindowInternal(object o)
        {
            object[] param = o as object[];
            //_progressView = new ProgressBarView((int)param[0], (string)param[1]);
            //_progressView.ShowDialog();
            App.MainView.InitProgressBar((int)param[0], (string)param[1]);
            System.Windows.Threading.Dispatcher.Run();
        }

        void folderList_FileRead(object sender, EventArgs e)
        {       
           // _progressView.IncrementBar();
            App.MainView.IncrementBar();
        }
    }
}
